﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SCRUM.Core.BusinessObjects;
using SCRUM.Core.DAO;
using System.Collections.Specialized;

/// <summary>
/// Shows/Manages detailed information about specific backlog item 
/// </summary>
public abstract partial class BliDetails : HierarchicalObjectDetails<BLI,UserStory>
{
    /// <summary>
    /// All Types of BLI
    /// </summary>
    private NameValueCollection allTypes;

    /// <summary>
    /// Loads types of BLI
    /// </summary>
    protected void LoadTypes()
    {
        Type.Items.Clear();
        allTypes = DAOBli.GetBliTypes();
        for (int i = 0; i < allTypes.Count; i++)
        {
            Type.Items.Add(new ListItem(allTypes.GetKey(i), allTypes.Get(i)));
        }
    }

    /// <summary>
    /// Loads the user control
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void Page_Load(object sender, EventArgs e)
    {
        this.LoadExtendedInputsFrom(PanelItemDetails);
        //LoadTypes();
    }

    /// <summary>
    /// Called when page is loaded
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void Page_Init(object sender, EventArgs e)
    {
        LoadTypes();
    }

    protected override IList<ScrumInput> LoadExtendedInputs()
    {
         return this.LoadExtendedInputsFrom(PanelItemDetails);
    }

    /// <summary>
    /// Loads the available superior items (=User Stories).
    /// </summary>
    protected override void LoadAvailableSuperiorItems()
    {
        AvailableSuperiorItems = DAOUserStory.GetAllUserStories();
    }

    /// <summary>
    /// Sets all data and visibility in BliDetails for selected bli
    /// </summary>
    /// <param name="bli">selected bli</param>
    /// <summary>
    /// Displays all fields of the selected item into textboxes in form.
    /// </summary>
    public override void DisplayItem(BLI bli)
    {
        this.Visible = true;

        this.UserStory.DataSource = AvailableSuperiorItems;
        this.UserStory.DataBind();

        bool exists = bli != null;

        this.Header.LastChange =            exists ? bli.LastChange     : DateTime.MinValue;
        this.Name.Text =                    exists ? bli.Name           : "";
        this.Description.Text =             exists ? bli.Description    : "";
        int bliTypeID =                     exists ? bli.TypeID         : 1;
        this.Priority.SelectedPriority =    exists ? bli.Priority       : 2;
        this.States.SelectedState =         exists ? bli.State          : State.DefaultState(State.StateTypes.Bli);
        this.ProgressBar.Progress =         exists ? bli.Progress       : 0;
        this.Estimation.Value =             exists ? bli.Estimation     : 0;
        this.Type.Items.FindByValue(bliTypeID.ToString()).Selected = true;
        int superiorItemID =                exists ? bli.SuperiorItemID : this.SuperiorItemID;
        if (superiorItemID != -1)
        {
            this.UserStory.SelectedValue = superiorItemID.ToString();
        }

        // enabled
        this.States.Enabled = exists;
        this.UserStory.Enabled = exists;

        // visibility
        this.ProgressBar.Visible = exists;

        // readonly
    }



    /// <summary>
    /// Creates new bli
    /// </summary>
    /// <returns>Created bli</returns>
    public override BLI CreateItem()
    {   
        int BliTypeID = System.Convert.ToInt32(Type.SelectedValue);
        //int BliStateID = System.Convert.ToInt32(State.SelectedValue);
        int UserStoryID = System.Convert.ToInt32(UserStory.SelectedValue);

        BLI bli = new BLI(
            0, 
            Name.Text, 
            BliTypeID, 
            Description.Text, 
            UserStoryID,
            this.States.SelectedState.ID,
            System.Convert.ToInt32(Estimation.Text),
            ProgressBar.Progress,
            Priority.SelectedPriority,
            DateTime.Now
        );
        bli = DAOBli.CreateNewBli(bli, UserStoryID);        
        return bli;
    }

    /// <summary>
    /// Edits bli according to data in bli detail panel
    /// </summary>
    /// <param name="relOld">Bli to update</param>
    public override BLI EditItem(int bliID)
    {
        int BliTypeID = System.Convert.ToInt32(this.Type.SelectedValue);
        //int BliStateID = System.Convert.ToInt32(this.State.SelectedValue);
        int UserStoryID = System.Convert.ToInt32(this.UserStory.SelectedValue);

        BLI bli = new BLI(
            bliID, 
            Name.Text, 
            BliTypeID, 
            Description.Text, 
            UserStoryID, 
            this.States.SelectedState.ID, 
            System.Convert.ToInt32(Estimation.Text), 
            ProgressBar.Progress, 
            Priority.SelectedPriority,
            DateTime.Now
        );
        DAOBli.UpdateBli(bli, UserStoryID);
        this.Visible = false;
        this.SuperiorItemID = UserStoryID;
        DisplayItem(bli);
        return bli;
    }

    /// <summary>
    /// Deletes bli
    /// </summary>
    /// <param name="bli">Bli to delete</param>
    public override void DeleteItem(BLI bli)
    {
        DAOBli.DeleteBli(bli);
        this.Visible = false;
    }

    /// <summary>
    /// Validates the input.
    /// </summary>
    /// <returns>Validation result</returns>
    public override bool ValidateInput()
    {
        throw new NotImplementedException();
        return true;
    }

    protected override Panel panelWithControls { get { return this.PanelItemDetails; } }
}
